﻿using System;
using System.Data;
using System.Windows.Forms;
using Home.Haushalt.Lib;
using Infragistics.Win.UltraWinCalcManager.FormulaBuilder;
using System.Drawing;
using Infragistics.Win.UltraWinGrid;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Home.Haushalt.Lib.Element;
using System.Collections;


namespace Home.Haushalt.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmGridForm : Form
    {
        /// <summary></summary>
        protected UcGridForm _ucGridControl;

        /// <summary></summary>
        private string _layoutName = "";

        /// <summary></summary>
        protected string LayoutName
        {
            get { return _layoutName; }
            set
            {
                _layoutName = value;

                string caption = _layoutName.Equals(DefaultColumnSets.GridDefault.ToString()) ? "[" + LO.GridContextMenu("Layout.DefaultName") + "]" : _layoutName;

                this.Text = LO.Common("FrmGridFormCaption", LO.ElementName(_ucGridControl.BaseGrid.ElementType), caption);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public FrmGridForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ucGridControl"></param>
        public FrmGridForm(UcGridForm ucGridControl)
        {
            InitializeComponent();

            _ucGridControl = ucGridControl;

            this.Controls.Add(_ucGridControl);

            txtFullTextSearch.Visible = !(ucGridControl is UcGridFormMonthbalance || ucGridControl is UcGridFormMain);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmGridForm_Load(object sender, EventArgs e)
        {
            Init();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Init()
        {
            if (_ucGridControl != null)
            {
                _ucGridControl.Dock = DockStyle.Fill;
                _ucGridControl.BringToFront();

                _ucGridControl.OnStatusChanged += new EventHandler<HEventArgs>(DAO_OnStatusMessage);
                _ucGridControl.OnLayoutChanged += new EventHandler<HEventArgs>(grid_OnLayoutChanged);

                LayoutName = _ucGridControl.LayoutName;

                if (!txtFullTextSearch.Text.Equals(string.Empty))
                {
                    FullTextSearch();
                }
            }

            //Nicht vergessen onformclose wieder rauszunehmen
            DAO.OnElementTableSave += new EventHandler<HEventArgs>(DAO_OnStatusMessage);
            DAO.OnElementTableSaved += new EventHandler<HEventArgs>(DAO_OnStatusMessage);
            DAO.OnStatusMessage += new EventHandler<HEventArgs>(DAO_OnStatusMessage);

            Journal.OnActionResultEscalation += new EventHandler<Journal.ActionResultEscalationEventArgs>(Journal_OnActionResultEscalation);

            SetStatusImage();

            MenuBind();

            WindowController.Register(this);
        }

        #region MENU METHODEN

        /// <summary>
        /// 
        /// </summary>
        protected void MenuBind()
        {
            if (DAO.Credential.AccessAllowed)
            {
                //Element-Menü
                SortedList<int, Type> elementMenus = new SortedList<int, Type>();
                //Erst sortieren
                foreach (Type elementType in DAO.ElementTypes.Keys)
                {
                    ElementExtensionAttribute attr = DAO.ElementTypes[elementType];
                    if (attr.Group.Equals(ElementBase.ELEMENTGROUP_ELEMENT) && attr.MainMenuSort > -1)
                    {
                        elementMenus.Add(attr.MainMenuSort, elementType);
                    }
                }

                //Dann Elemente hinzufügen
                foreach (Type elementType in elementMenus.Values)
                {
                    CreateElementLayoutMenu(elementType);
                }

                //Property-Menü
                mnuProperties.DropDownItems.Clear();
                foreach (Type elementType in DAO.ElementTypes.Keys)
                {
                    ElementExtensionAttribute attr = DAO.ElementTypes[elementType];
                    if (attr.Group == ElementBase.ELEMENTGROUP_PROPERTY)
                    {
                        CreatePropertyMenu(elementType);
                    }
                }

                //UserMenu
                if (DAO.Settings.Items.Count > 1)
                {
                    //Alte entfernen
                    foreach (ToolStripItem item in mnuFile.DropDownItems.Cast<ToolStripItem>().Where(item => item.Tag is CSettings).ToList())
                    {
                        mnuFile.DropDownItems.Remove(item);
                    }

                    //Neue erstellen
                    ToolStripSeparator settingsBlock = new ToolStripSeparator();
                    settingsBlock.Tag = DAO.Settings.Selected;
                    mnuFile.DropDownItems.Add(settingsBlock);

                    foreach (CSettings settings in DAO.Settings.Items.Where(item => !item.UserName.Equals(DAO.Settings.Selected.UserName)))
                    {
                        ToolStripMenuItem userMenuItem = new ToolStripMenuItem(LO.GridContextMenu("File.LoginAs", settings.UserName), null, LoadUser_Click);
                        userMenuItem.Tag = settings;

                        mnuFile.DropDownItems.Add(userMenuItem);
                    }
                }

                //UpdateBankingMenu();
            }
        }

        #region PROPERTIES

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementType"></param>
        protected void CreatePropertyMenu(Type elementType)
        {
            ToolStripMenuItem propertyMenuItem = new ToolStripMenuItem(elementType.Name.ToString(), null, LoadProperty_Click);
            propertyMenuItem.Tag = elementType;

            mnuProperties.DropDownItems.Add(propertyMenuItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadProperty_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem layoutItem = (ToolStripMenuItem)sender;
            Type layoutElement = (Type)layoutItem.Tag;

            Open(layoutElement, DefaultColumnSets.GridDefault.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadUser_Click(object sender, EventArgs e)
        {
            //Reset der Daten vom aktuellen Benutzer
            //Die Stelle und idie Reihenfolge sind wichtig (sonst werden Tabellen der falschen Benutzer überschrieben)!
            WindowController.Reset();
            DAO.Reset();
            LayoutController.Reset();

            ToolStripMenuItem propertyMenuItem = (ToolStripMenuItem)sender;
            string userName = ((CSettings)propertyMenuItem.Tag).UserName;

            FrmMain frmMain = (FrmMain)WindowController.FistGridForm;
            frmMain.Focus();
            frmMain.ShowLogin(LoginModes.Normal, userName);
        }

        #endregion PROPERTIES

        #region ELEMENTS

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementType"></param>
        private void CreateElementLayoutMenu(Type elementType)
        {
            ToolStripDropDownButton elementButton = toolStrip.Items.Cast<ToolStripItem>().Where(item => item.Tag == elementType).Cast<ToolStripDropDownButton>().FirstOrDefault();
            if (elementButton != default(ToolStripDropDownButton))
            {
                toolStrip.Items.Remove(elementButton);
                elementButton.Dispose();
            }

            elementButton = new ToolStripDropDownButton(LO.ElementName(elementType));
            elementButton.DropDownOpened += new EventHandler(elementButton_DropDownOpened);
            elementButton.Tag = elementType;
            toolStrip.Items.Add(elementButton);

            UpdateElementLayoutMenu(elementButton);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void elementButton_DropDownOpened(object sender, EventArgs e)
        {
            ToolStripDropDownButton elementButton = (ToolStripDropDownButton)sender;
            //ToolStripTextBox tb = elementButton.DropDownItems[elementButton.DropDownItems.Count - 1] as ToolStripTextBox;
            ToolStripTextBox tb = elementButton.DropDownItems[0] as ToolStripTextBox;
            tb.Focus();
            tb.SelectAll();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementType"></param>
        public void UpdateElementLayoutMenu(Type elementType)
        {
            foreach (ToolStripItem item in toolStrip.Items)
            {
                if (item.Tag != null && item.Tag is Type)
                {
                    Type oelement = (Type)item.Tag;
                    if (oelement == elementType)
                    {
                        UpdateElementLayoutMenu(item);
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementButton"></param>
        private void UpdateElementLayoutMenu(ToolStripItem elementButtonItem)
        {
            ToolStripDropDownButton elementButton = (ToolStripDropDownButton)elementButtonItem;

            elementButton.DropDownItems.Clear();
            Type elementType = (Type)elementButton.Tag;

            //Such-Textbox
            ToolStripTextBox searchItem = new ToolStripTextBox("Suchen");
            searchItem.Text = txtFullTextSearch.Text;
            elementButton.DropDownItems.Add(searchItem);
            searchItem.KeyUp += new KeyEventHandler(searchItem_KeyUp);

            //Einzelne Sichte
            foreach (DataRow row in DAO.GetRows(GridLayout.ElementType, "{0} = '{1}' and {2} <> '{3}' and {2} <> '{4}'", GridLayout.Fields.Table, elementType.Name, GridLayout.Fields.Name, DefaultColumnSets.GridDefault.ToString(), DefaultColumnSets.TabDefault.ToString()))
            {
                //Icon finden und initialisieren
                Image image = null;
                if (row[GridLayout.Fields.Icon] != null && !row[GridLayout.Fields.Icon].ToString().Equals(string.Empty))
                {
                    image = Image.FromFile(Path.Combine(DAO.HFile.IconsFullPath, row[GridLayout.Fields.Icon].ToString()));
                }

                ToolStripMenuItem layoutItem = new ToolStripMenuItem(row[GridLayout.Fields.Name].ToString(), image);
                layoutItem.MouseUp += new MouseEventHandler(layoutItem_MouseUp);
                layoutItem.Tag = row[GridLayout.Fields.Name];
                //layoutItem.AllowDrop = true;
                //layoutItem.DragEnter += new DragEventHandler(layoutItem_DragEnter);
                elementButton.DropDownItems.Add(layoutItem);
            }

            //Standard-Sicht
            ToolStripMenuItem defaultLayoutItem = new ToolStripMenuItem(LO.GridContextMenu("Layout.DefaultName"), global:: Home.Haushalt.Lib.Properties.Resources.element_layout_default, LoadElementLayout_Click);
            defaultLayoutItem.Tag = DefaultColumnSets.GridDefault;
            elementButton.DropDownItems.Add(defaultLayoutItem);


            //Favorit-Elemente
            List<ElementBase> historyElements = WindowController.ElementHistory.Where(item => item.GetType().Equals(elementType)).ToList();
            if (!historyElements.Count.Equals(0))
            {
                elementButton.DropDownItems.Add(new ToolStripSeparator());
            }

            foreach (ElementBase historyElement in historyElements)
            {
                ToolStripMenuItem elementItem = new ToolStripMenuItem(historyElement.PropertyFormCaption, global:: Home.Haushalt.Lib.Properties.Resources.element_layout_user);
                elementItem.MouseUp += new MouseEventHandler(historyElement_MouseUp);
                elementItem.Tag = historyElement;
                elementButton.DropDownItems.Add(elementItem);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchItem_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Enter))
            {
                OpenElementLayout_Click(sender, null);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void historyElement_MouseUp(object sender, MouseEventArgs e)
        {
            ToolStripMenuItem elementItem = (ToolStripMenuItem)sender;
            ElementBase historyElement = (ElementBase)elementItem.Tag;

            FrmProperty frmProperty = WindowController.EditElementProperty(this, new DataRow[] { historyElement.Row }.ToList());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void layoutItem_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button.Equals(MouseButtons.Left))
            {
                LoadElementLayout_Click(sender, null);
            }

            if (e.Button.Equals(MouseButtons.Right))
            {
                OpenElementLayout_Click(sender, null);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadElementLayout_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem layoutItem = (ToolStripMenuItem)sender;
            Type layoutElement = (Type)layoutItem.OwnerItem.Tag;
            string layoutName = ((ToolStripMenuItem)sender).Tag.ToString();

            LoadElementLayout(layoutElement, layoutName, layoutElement != _ucGridControl.BaseGrid.ElementType);

            ((ToolStripDropDownButton)layoutItem.OwnerItem).HideDropDown();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenElementLayout_Click(object sender, EventArgs e)
        {
            ToolStripItem layoutItem = (ToolStripItem)sender;
            Type layoutElement = (Type)layoutItem.OwnerItem.Tag;
            string layoutName = (sender is ToolStripTextBox) ? "search:" + ((ToolStripTextBox)sender).Text : layoutItem.Tag.ToString();

            LoadElementLayout(layoutElement, layoutName, true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="layoutElement"></param>
        /// <param name="layoutName"></param>
        /// <param name="newWindow"></param>
        private void LoadElementLayout(Type layoutElement, string layoutName, bool newWindow)
        {
            if (newWindow)
            {
                Open(layoutElement, layoutName);
            }
            else
            {
                _ucGridControl.BaseGrid.LoadLayout(layoutName);

                LayoutName = layoutName;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementType"></param>
        /// <param name="layoutName"></param>
        public void Open(Type elementType, string layoutName)
        {
            string searchstring = "";
            if (layoutName.StartsWith("search:"))
            {
                searchstring = layoutName.Split(new char[] { ':' }, 2)[1];
                layoutName = DefaultColumnSets.GridDefault.ToString();
            }

            FrmGridForm frmGridForm = new FrmGridForm(UcGridForm.Instatiate(elementType, DefaultColumnSets.GridDefault, layoutName));
            frmGridForm.txtFullTextSearch.Text = searchstring;
            frmGridForm.Location = new Point(this.Left + 50, this.Top + 50);

            frmGridForm.Show();
        }

        #endregion ELEMENTS

        /*#region BANKING

        /// <summary></summary>
        private ToolStripDropDownButton _bankingMenuButton;

        /// <summary>
        /// 
        /// </summary>
        public void UpdateBankingMenu()
        {
            if (_bankingMenuButton == null)
            {
                _bankingMenuButton = new ToolStripDropDownButton("Bank-Kontos");
                _bankingMenuButton.Tag = FinDate.ElementType;

                toolStrip.Items.Add(_bankingMenuButton);
            }

            _bankingMenuButton.DropDownItems.Clear();

            foreach (DataRow row in DAO.Tables[FinAccount.ElementType].Rows)
            {
                CreateBankingKontoMenu(row);
            }

            _bankingMenuButton.DropDownItems.Add(new ToolStripSeparator());

            foreach (DataRow row in DAO.GetRows(GridLayout.ElementType, "{0} = '{1}' and {2} <> '{3}'", GridLayout.Fields.Table, FinDate.ElementType.Name, GridLayout.Fields.Name, DefaultColumnSets.GridDefault))
            {
                ToolStripMenuItem layoutItem = new ToolStripMenuItem(row[GridLayout.Fields.Name].ToString(), null);
                layoutItem.MouseUp += new MouseEventHandler(layoutItem_MouseUp);
                layoutItem.Tag = row[GridLayout.Fields.Name];
                _bankingMenuButton.DropDownItems.Add(layoutItem);
            }

            _bankingMenuButton.DropDownItems.Add(new ToolStripSeparator());
            ToolStripMenuItem defaultLayoutItem = new ToolStripMenuItem("Standard", null, LoadBankingLayout_Click);
            defaultLayoutItem.Tag = DefaultColumnSets.GridDefault;
            _bankingMenuButton.DropDownItems.Add(defaultLayoutItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="row"></param>
        private void CreateBankingKontoMenu(DataRow row)
        {
            FinAccount accoutnObj = new FinAccount(row);

            ToolStripMenuItem ktoMenuItem = new ToolStripMenuItem(accoutnObj.Bank + "-" + accoutnObj.Name, null, LoadBankingKontoData_Click);
            ktoMenuItem.Tag = row;

            _bankingMenuButton.DropDownItems.Add(ktoMenuItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadBankingKontoData_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem layoutItem = (ToolStripMenuItem)sender;
            DataRow row = (DataRow)layoutItem.Tag;

            UcGridFormElement frmGridForm = new UcGridFormElement(FinDate.ElementType, DefaultColumnSets.GridDefault, DefaultColumnSets.GridDefault.ToString());
            frmGridForm.Show();

            //((ToolStripDropDownButton)layoutItem.OwnerItem).HideDropDown();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadBankingLayout_Click(object sender, EventArgs e)
        {
            Type layoutElement = FinAccount.ElementType;
            string layoutName = ((ToolStripMenuItem)sender).Tag.ToString();

            _ucGridControl.BaseGrid.LoadLayout(layoutName);

            LayoutName = layoutName;

            _bankingMenuButton.HideDropDown();
        }

        #endregion BANKING*/


        #endregion LAYOUT MANAGEMENT METHODEN

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_OnLayoutChanged(object sender, HEventArgs e)
        {
            LayoutName = e.Tag.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Journal_OnActionResultEscalation(object sender, Journal.ActionResultEscalationEventArgs e)
        {
            WindowController.SetStatusImage();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DAO_OnStatusMessage(object sender, HEventArgs e)
        {
            statusLabel.Text = e.Action;

            if (e.ProcessMax != -1)
            {
                statusProcessBar.Visible = true;
                statusProcessBar.Maximum = e.ProcessMax;
                statusProcessBar.Value = e.ProcessIndex;
            }
            else
            {
                statusProcessBar.Visible = false;
            }

            statusStrip.Refresh();

            Application.DoEvents();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <param name="args"></param>
        protected void SetStatusText(string label, params object[] args)
        {
            statusLabel.Text = LO.Status(label, args);
            Application.DoEvents();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public void SetStatusImage()
        {
            Image img = global:: Home.Haushalt.Lib.Properties.Resources.log_ok;
            statusIco.Text = "";

            if (Journal.ErrorCount > 0)
                img = global:: Home.Haushalt.Lib.Properties.Resources.log_nok;
            else if (Journal.WarningCount > 0)
                img = global:: Home.Haushalt.Lib.Properties.Resources.log_warning;
            else
                img = global:: Home.Haushalt.Lib.Properties.Resources.log_ok;

            statusIco.Image = img;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmGridForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            DAO.OnElementTableSave -= new EventHandler<HEventArgs>(DAO_OnStatusMessage);
            DAO.OnElementTableSaved -= new EventHandler<HEventArgs>(DAO_OnStatusMessage);
            DAO.OnStatusMessage -= new EventHandler<HEventArgs>(DAO_OnStatusMessage);
            Journal.OnActionResultEscalation -= new EventHandler<Journal.ActionResultEscalationEventArgs>(Journal_OnActionResultEscalation);

            WindowController.Unregister(this);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuCloseAll_Click(object sender, EventArgs e)
        {
            WindowController.CloseAll();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void statusIco_DoubleClick(object sender, EventArgs e)
        {
            Journal.Show(false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuSaveAllChanges_Click(object sender, EventArgs e)
        {
            DAO.Save();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmGridForm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode.Equals(Keys.S))
            {
                mnuSaveAllChanges_Click(sender, null);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuFile_DropDownOpening(object sender, EventArgs e)
        {
            mnuSaveAllChanges.Enabled = DAO.DataChanged;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFullTextSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Enter))
            {
                FullTextSearch();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void FullTextSearch()
        {
            _ucGridControl.BaseGrid.DisplayLayout.Bands[0].ColumnFilters.ClearAllFilters();
            if (!txtFullTextSearch.Text.Equals(string.Empty))
            {
                _ucGridControl.BaseGrid.DisplayLayout.Bands[0].ColumnFilters.LogicalOperator = FilterLogicalOperator.Or;
                foreach (ColumnFilter docuIdFilter in _ucGridControl.BaseGrid.DisplayLayout.Bands[0].ColumnFilters)
                {
                    docuIdFilter.LogicalOperator = FilterLogicalOperator.And;
                    docuIdFilter.FilterConditions.Add(new FilterCondition(FilterComparisionOperator.Like, "*" + txtFullTextSearch.Text + "*"));
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuDecryptFile_Click(object sender, EventArgs e)
        {
            if (ofdCryptFile.ShowDialog() == DialogResult.OK)
            {
                DAO.Credential.CurrentCry.FileDecrypt(ofdCryptFile.FileName);

                Chips.ShowExplorerAtFile(ofdCryptFile.FileName);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuEncryptFile_Click(object sender, EventArgs e)
        {
            if (ofdCryptFile.ShowDialog() == DialogResult.OK)
            {
                DAO.Credential.CurrentCry.FileEncrypt(ofdCryptFile.FileName);

                Chips.ShowExplorerAtFile(ofdCryptFile.FileName);
            }
        }

        private const string RezeptFolder = "Rezepte";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuRezeptExport_Click(object sender, EventArgs e)
        {
            //string rezeptRootFolder = Path.Combine(DAO.TempFullPath, RezeptFolder);

            //Chips.DirectoriesAndFilesTryDelete(rezeptRootFolder);

            //foreach (DataRow docuRow in DAO.Tables[Document.ElementType].Select(string.Format("{0} = '{1}'", Document.ELEMENTPROPERTY_LABEL, "Rezept")))
            //{
            //    Document docu = new Document(docuRow);

            //    string description = docu.Row[Document.Fields.Description].ToString();

            //    List<string> includeExtnesion = new List<string>(new string[] { ".txt" });

            //    foreach (DataRow pageRow in DAO.Tables[Page.ElementType].Select(string.Format("{0} = {1}", Page.Fields.DocumentId, docu.ID)))
            //    {
            //        Page page = new Page(pageRow);

            //        if (includeExtnesion.Contains(Path.GetExtension(page.FilePath)))
            //        {
            //            string[] targetFileNameParts = description.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);


            //            string rezeptFolder = rezeptRootFolder;
            //            if (targetFileNameParts.Length > 1)
            //            {
            //                for (int i = 0; i < targetFileNameParts.Length - 1; i++)
            //                {
            //                    rezeptFolder = Path.Combine(rezeptFolder, targetFileNameParts[i].Trim());
            //                }
            //            }

            //            if (!Directory.Exists(rezeptFolder))
            //                Directory.CreateDirectory(rezeptFolder);

            //            string fileName = targetFileNameParts[targetFileNameParts.Length - 1] + Path.GetExtension(page.FilePath);
            //            string targetFullFileName = Path.Combine(rezeptFolder, fileName);

            //            int index = 1;
            //            while (File.Exists(targetFullFileName))
            //                targetFullFileName = Path.Combine(rezeptFolder, targetFileNameParts[targetFileNameParts.Length - 1] + " - (" + (index++).ToString() + ")" + Path.GetExtension(page.FilePath));


            //            File.Copy(page.FilePath, targetFullFileName);
            //        }
            //    }
            //}

            //Chips.ShowExplorerAtFolder(rezeptRootFolder);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmGridForm_Activated(object sender, EventArgs e)
        {
            if (txtFullTextSearch.Enabled)
            {
                txtFullTextSearch.Focus();
            }

            //Program.StartArgs[Program.STARTARG_PASSWORD] = string.Empty;

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuExit_Click(object sender, EventArgs e)
        {
            WindowController.CloseApplication();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuExitWithoutSave_Click(object sender, EventArgs e)
        {
            DAO.ClearChangedElements();
            WindowController.CloseApplication();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuIntegrityCheck_Click(object sender, EventArgs e)
        {
            FrmDataIntegrityCheck frmDataIntegrityCheck = new FrmDataIntegrityCheck();
            frmDataIntegrityCheck.ShowDialog();
        }


    }
}
